package com.yunxitest.yunxitest.item.typec

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import com.yunxitest.yunxitest.Interface.IResultCallback
import com.yunxitest.yunxitest.utils.L

/**
 * Created by wangkm on 2018/3/14.
 * 电池电量监听广播
 */
class BatteryBroadcastReceiver(val callBack: IResultCallback): BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {

        if (intent.action == Intent.ACTION_BATTERY_CHANGED) {
            val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN)
            if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
             // todo 充电中
                callBack.success()
                L.d("testItem"," typec: charing")
            } else {
                callBack.fail(" no charing")
                L.d("testItem"," typec: not  charing")
            }

            //得到系统当前电量
            val level = intent.getIntExtra("level", 0)
            //取得系统总电量
            val total = intent.getIntExtra("scale", 100)
            val power = level * 100 / total
        }

    }

}
